<?php

namespace brand\traits;

use brand\Brand;
use brand\logic\BrandHelper;
use brand\page\Form;

trait Component
{

    public function _init()
    {
        
    }

    public function Form()
    {
        $component = $this->component;
        $this->form_column = Form::$SurfaceForm->$component($this->field,$this->label,$this->value);
        $this->columnsCheckPublic();
        return $this->form_column;

    }

    public function Table($this_column)
    {

        $props = $this->issetTableValue('props') ?: [];
        $props['show-overflow-tooltip'] = true;
        if (!isset($props['width']) && strstr($this->field,'time') !== false){
            $props['width'] = 150;
        }

//        $props = $this->issetTableValue('props') ?: [];
        $append_props = $this->issetTableValue('append_props') ?: [];
        $props = array_merge($props,$append_props);
        if(!isset($props['align'])) $props['align'] = 'center';
        if(!isset($props['header-align'])) $props['header-align'] = 'center';
        if ($props){
            $this_column->props($props);
        }
        return $this_column;
    }


    /**
     * 表单构建验证规则
     * @return void
     */
    public function buildVilidate()
    {

        if ($this->is_search){
            return;
        }
        $validate = [];
        if (isset($this->config['form']['validate'])){
            $validateConfig = $this->config['form']['validate'];
            if(is_array($validateConfig)){
                foreach ($validateConfig as $item) {
                    if ( isset($item['required']) && !isset($item['message'])){
                        $item['message'] = $this->label . ' 必填';
                    }
                    $validate[] = $item;
                }
            }

            $this->config['form']['validate'] = $validate;
        }
    }


    /**
     * 构建Props
     */
    public function buildProps()
    {

        $form_props = $this->config['form']['props'] ?? [];
        $form_extend_props = $this->config['form']['append_props'] ?? [];
        foreach ($form_extend_props as $key => $form_extend_prop) {
            if (!isset($form_props[$key])) $form_props[$key] = $form_extend_prop;
        }
        $this->config['form']['props'] = $form_props;

        $table_props = $this->config['table']['props'] ?? [];
        $table_extend_props = $this->config['table']['extend_props'] ?? [];
        foreach ($table_extend_props as $key => $table_extend_prop) {
            if (!isset($table_props[$key])) $table_props[$key] = $table_extend_prop;
        }
        $this->config['table']['props'] = $table_props;
        $this->props = $this->config['form']['props'];

    }


    /**
     * 行设置
     */
    public function columnsCheckPublic()
    {

        $arr = ['marker','visible','options','props'];
        if (!$this->is_search){
            array_push($arr,'validate');
        }
        if ($this->component == 'arrays'){
            unset($arr[array_search('options',$arr)]);
        }
        foreach ($arr as $item) {
            if ($this->getFormParam($item)){
                $this->form_column->$item($this->getFormParam($item));
            }
        }

    }

    /**
     * 表格字段过滤
     * @param $key
     * @param $value
     */
    public function checkTableValue($key,$value)
    {

        return $value;
    }

    /**
     * 编辑/新增查询字段过滤
     */
    public function checkFormValue()
    {

    }

    /**
     * 更新/新增提交字段过滤
     * @param $key
     * @param $value
     */
    public function checkUpdateData($key, $value)
    {
        return $value;
    }


    /**
     * 数据表字段创建CURD
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {
        return Brand::Builder()->form_props($props);
    }


    /**
     * 构建搜索条件
     * @param $key
     * @param $param
     * @return array|false
     */
    public function buildCondition($key,$param)
    {
        [$field,$key] = $this->getSearchField($key);

        $condition = $this->config['search']['condition'] ?? '';
        if (!$condition) return false;

        //TODO 搜索条件
        if ($condition == 'like'){
            $param = "%$param%";
        }elseif ($condition == 'between' ){
            if (!is_array($param)) $condition = "=";
        }
        return [$field . '.' .$key,$condition,$param];
    }

    public function getSearchField($key){
        $keys = explode('__BA__',$key);
        if (count($keys) == 2){
            $key = $keys[1];
            $field = $keys[0];
        }else{
            $field = $this->field;
        }
        return [$field,$key];
    }

    /**
     * 编辑字段
     * @param $tableConfigValue
     * @return array
     */
    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        return ['column' => [],'value' => []];

    }

    /**
     * 提交数据转curd配置
     * @param $params
     */
    public function paramsToCurdConf($params,$config,$tableConfig)
    {
        $form = [];
        $table = [];

        //表单方法
        if (isset($params['curd_method']) && $params['curd_method']){
            $form['method'] = $params['curd_method'];
        }
        //表单是否必填
        if (isset($params['validate_required']) && $params['validate_required'] == 1){
            $form['validate'] = [ ['required' => 1] ];
        }
        //提示信息
        if (isset($params['marker']) && $params['marker']){
            $form['marker'] = $params['marker'];
        }
        //其他特定配置
        [$form,$table] = $this->methodFormConf($form,$table,$params);


        //额外props
        $form['append_props'] = BrandHelper::encodeOptions($params['form_append_props']);
        $table['append_props'] = BrandHelper::encodeOptions($params['table_append_props']);

        //表格排序
        $table['props']['sortable'] = (bool)$params['table_order'];
        //表格显示
        $table['status'] = (isset($params['table_show']) && $params['table_show']);
        //表格显示类型
        $table['show_type'] = $params['show_type'] ?? "";
        //表格宽度
        if (isset($params['table_width']) && $params['table_width'] > 0){
            $table['props']['width'] = (int)$params['table_width'];
        }else{
            if (isset($table['props']['width'])) unset($table['props']['width']);
        }


        //表单显示
        $form['status'] = (isset($params['form_show']) && $params['form_show']);



        //特殊字符
        $config['special_type'] = $params['special_type'] ?? '';

        $config['form'] = $form;
        $config['table'] = $table;
        if (isset($params['search']) && $params['search']){
            $config['search'] = ['condition' => $params['search']];
        }else{
            $config['search'] = false;
        }

        $config['title'] = $params['title'] ?? "";

        $tableConfig[$params['field']] = $config;

        //排序
        if (isset($params['sort']) && $params['sort']){
            $tableConfig = BrandHelper::columnsInsertArray($tableConfig,$params['sort'],[$params['field'] => $config]);
        }

        return $tableConfig;
    }

    public function methodFormConf($form,$table,$params)
    {
        return [$form,$table];
    }



}